package javabase.concurrent;

public class SynchronizedTest {
	
	
	public static void main(String[] args) {
		Outputer outputer = new Outputer();
		
		new Thread(() -> {
			while (true) {
				outputer.output1("dek");
			}
		}).start();
		
		
		new Thread(() -> {
			while(true) {
				outputer.output2("haq");
			}
		}).start();
	}
	
	static class Outputer {
		public void output1(String name) {
			synchronized (this) {
				for (int i = 0; i < name.length(); i++) {
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}
		
		public synchronized void output2(String name) {
			for (int i = 0; i < name.length(); i++) {
				System.out.print(name.charAt(i));
			}
			System.out.println();
		}
		
		/**
		 *     同字节码对象 Outputer.class
		 * @param name
		 */
		public static synchronized void output3(String name) {
			for (int i = 0; i < name.length(); i++) {
				System.out.print(name.charAt(i));
			}
			System.out.println();
		}
		
	}
	
}
